package me.firstapp.spi.shop.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import me.firstapp.common.exception.ServiceException;
import me.firstapp.common.exception.StatusHouse;
import me.firstapp.common.json.JsonPage;
import me.firstapp.common.json.ListObject;
import me.firstapp.common.json.PageObject;
import me.firstapp.common.json.SingleObject;
import me.firstapp.common.module.shop.JsonProductSkuRelation;
import me.firstapp.spi.base.impl.BaseServiceImpl;
import me.firstapp.spi.shop.ProductSkuRelService;

@Service
public class ProductSkuRelServiceImpl extends BaseServiceImpl implements ProductSkuRelService {

	@Override
	public SingleObject<JsonProductSkuRelation> addProductSkuRelation(Long productId, String skuName1,
			String skuValue1, String skuName2, String skuValue2, Integer stock, Double price) {
		SingleObject<JsonProductSkuRelation> result = new SingleObject<>();
		try {
			Map<String, Object> parameters = new HashMap<>();
			parameters.put("productId", productId);
			parameters.put("skuName1", skuName1);
			parameters.put("skuValue1", skuValue1);
			parameters.put("skuName2", skuName2);
			parameters.put("skuValue2", skuValue2);
			parameters.put("stock", stock);
			parameters.put("price", price);

			JSONObject resultJson = api(parameters, "/productSkuRelation/addProductSkuRelation.do", HttpMethod.POST,
					"新增商品属性关系");
			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(resultJson.getString("code"))) {
				JsonProductSkuRelation apiJsonProductSkuRelation = JSONObject
						.toJavaObject(resultJson.getJSONObject("object"), JsonProductSkuRelation.class);
				result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
				result.setObject(apiJsonProductSkuRelation);
			} else {
				throw new ServiceException(resultJson.getString("code"), resultJson.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;
	}

	@Override
	public PageObject<JsonProductSkuRelation> findApiJsonProductSkuRelationPage(Integer pageNo, Integer pageSize,
			Map<String, Object> searchParams, String sortParam, String sortType) {
		PageObject<JsonProductSkuRelation> result = new PageObject<>();
		try {
			Map<String, Object> parameters = new HashMap<>();
			parameters.put("pageNo", pageNo);
			parameters.put("pageSize", pageSize);
			parameters.put("sortParam", sortParam);
			parameters.put("sortType", sortType);
			JSONObject jsonObject = apiBody(parameters, "/productSkuRelation/findProductSkuRelationPage.do",
					"分页查询商品属性关系", searchParams);
			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(jsonObject.getString("code"))) {
				JSONObject pageJsonObject = jsonObject.getJSONObject("page");
				if (pageJsonObject != null) {
					JsonPage<JsonProductSkuRelation> jsonPage = new JsonPage<>();
					jsonPage.setPageNo(pageJsonObject.getIntValue("pageNo"));
					jsonPage.setPageSize(pageJsonObject.getIntValue("pageSize"));
					jsonPage.setTotalCount(pageJsonObject.getIntValue("totalCount"));
					List<JsonProductSkuRelation> skuRelList = new ArrayList<>();
					JSONArray jsonArray = pageJsonObject.getJSONArray("list");
					if (jsonArray != null) {
						for (int i = 0; i < jsonArray.size(); i++) {
							JSONObject _jsonObject = jsonArray.getJSONObject(i);
							if (_jsonObject != null) {
								JsonProductSkuRelation rel = JSONObject.toJavaObject(_jsonObject,
										JsonProductSkuRelation.class);
								skuRelList.add(rel);
							}
						}
					}
					jsonPage.setList(skuRelList);
					result.setPage(jsonPage);
					result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
				}
			} else {
				throw new ServiceException(jsonObject.getString("code"), jsonObject.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;
	}

	@Override
	public SingleObject<Object> deleteProductSkuRelations(String ids) {
		SingleObject<Object> result = new SingleObject<>();
		try {
			Map<String, Object> parameters = new HashMap<>();
			parameters.put("productSkuRelationsIds", ids);

			JSONObject resultJson = api(parameters, "/productSkuRelation/deleteProductSkuRelations.do", HttpMethod.POST,
					"删除商品属性关系");
			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(resultJson.getString("code"))) {
				result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			} else {
				throw new ServiceException(resultJson.getString("code"), resultJson.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;
	}

	@Override
	public SingleObject<JsonProductSkuRelation> updateProductSkuRelation(Long productSkuRelationId, String skuName1,
			String skuValue1, String skuName2, String skuValue2, Integer stock, Double price) {
		SingleObject<JsonProductSkuRelation> result = new SingleObject<>();
		try {
			Map<String, Object> parameters = new HashMap<>();
			parameters.put("productSkuRelationId", productSkuRelationId);
			parameters.put("skuName1", skuName1);
			parameters.put("skuValue1", skuValue1);
			parameters.put("skuName2", skuName2);
			parameters.put("skuValue2", skuValue2);
			parameters.put("stock", stock);
			parameters.put("price", price);

			JSONObject resultJson = api(parameters, "/productSkuRelation/updateProductSkuRelation.do", HttpMethod.POST,
					"更新商品属性关系");
			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(resultJson.getString("code"))) {
				JsonProductSkuRelation apiJsonProductSkuRelation = JSONObject
						.toJavaObject(resultJson.getJSONObject("object"), JsonProductSkuRelation.class);
				result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
				result.setObject(apiJsonProductSkuRelation);
			} else {
				throw new ServiceException(resultJson.getString("code"), resultJson.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;
	}

	@Override
	public SingleObject<JsonProductSkuRelation> findProductSkuRelationById(Long productSkuRelationId) {
		SingleObject<JsonProductSkuRelation> result = new SingleObject<>();
		try {
			Map<String, Object> parameters = new HashMap<>();
			parameters.put("productSkuRelationId", productSkuRelationId);

			JSONObject resultJson = api(parameters, "/productSkuRelation/findProductSkuRelationById.do", HttpMethod.GET,
					"通过id查询商品属性关系");
			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(resultJson.getString("code"))) {
				JsonProductSkuRelation apiJsonProductSkuRelation = JSONObject
						.toJavaObject(resultJson.getJSONObject("object"), JsonProductSkuRelation.class);
				result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
				result.setObject(apiJsonProductSkuRelation);
			} else {
				throw new ServiceException(resultJson.getString("code"), resultJson.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;
	}

	@Override
	public SingleObject<JsonProductSkuRelation> setDefaultProductSkuRelation(Long productSkuRelationId) {
		SingleObject<JsonProductSkuRelation> result = new SingleObject<>();
		try {
			Map<String, Object> parameters = new HashMap<>();
			parameters.put("productSkuRelationId", productSkuRelationId);

			JSONObject resultJson = api(parameters, "/productSkuRelation/setDefaultProductSkuRelation.do",
					HttpMethod.POST, "设置默认商品属性关系");
			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(resultJson.getString("code"))) {
				JsonProductSkuRelation apiJsonProductSkuRelation = JSONObject
						.toJavaObject(resultJson.getJSONObject("object"), JsonProductSkuRelation.class);
				result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
				result.setObject(apiJsonProductSkuRelation);
			} else {
				throw new ServiceException(resultJson.getString("code"), resultJson.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;
	}

	@Override
	public ListObject<JsonProductSkuRelation> findProductSkuRelationsByProductId(Long productId, Integer isDefault) {
		ListObject<JsonProductSkuRelation> result = new ListObject<JsonProductSkuRelation>();
		try{
			Map<String, Object> parameters = new HashMap<>();
			parameters.put("productId", productId);
			parameters.put("isDefault", isDefault);
			
			JSONObject resultJson = api(parameters, "/productSkuRelation/findSkuRelByProductId.do",
					HttpMethod.GET, "查询商品属性关系");
			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(resultJson.getString("code"))) {
				JSONArray jsonArray = resultJson.getJSONArray("items");
				List<JsonProductSkuRelation> rels = new ArrayList<JsonProductSkuRelation>();
				if (jsonArray != null) {
					for (int i = 0; i < jsonArray.size(); i++) {
						JSONObject _jsonObject = jsonArray.getJSONObject(i);
						if(_jsonObject != null){
							JsonProductSkuRelation rel = JSONObject.toJavaObject(_jsonObject, JsonProductSkuRelation.class);
							rels.add(rel);
						}
					}
				}
				result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
				result.setItems(rels);
			} else {
				throw new ServiceException(resultJson.getString("code"), resultJson.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;
	}
	
}
